import sys
sys.path.append('../')
import time

import RPi.GPIO  as  GPIO
GPIO.setmode(GPIO.BCM)
#设置GPIO引脚编号的模式，这里引脚编号采用BCM编码方式；
#BCM侧重CPU使用
num=[0,0,0,0]
i=0
dat=0

GPIO.setup(4,GPIO.OUT)
GPIO.output(4,GPIO.HIGH)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,GPIO.HIGH)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,GPIO.HIGH)

GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.HIGH)
GPIO.setup(23,GPIO.OUT)
GPIO.output(23,GPIO.HIGH)
GPIO.setup(24,GPIO.OUT)
GPIO.output(24,GPIO.HIGH)

GPIO.setup(16,GPIO.OUT)
GPIO.output(16,GPIO.HIGH)
GPIO.setup(20,GPIO.OUT)
GPIO.output(20,GPIO.HIGH)
GPIO.setup(21,GPIO.OUT)
GPIO.output(21,GPIO.HIGH)

GPIO.setup(13,GPIO.OUT)
GPIO.output(13,GPIO.HIGH)
GPIO.setup(19,GPIO.OUT)
GPIO.output(19,GPIO.HIGH)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.HIGH)
#初始化全部LED为高电平

from DFRobot_ADS1115 import ADS1115
ADS1115_REG_CONFIG_PGA_6_144V        = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V        = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V        = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V        = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V        = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V        = 0x0A # 0.256V range = Gain 16
ads1115 = ADS1115()

while True :
    
    # Set the IIC address
    # 设置 I2C 使用的地址
    # ADS1115_IIC_ADDRESS0
    ads1115.setAddr_ADS1115(0x48)
    # Sets the gain and input voltage range.
    # 设置增益和输入电压范围。
    # 6V输入
    ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
    # Get the Digital Value of Analog of selected channel
    # 获取所选通道的模拟量数字值
    # 使用频道0，AIN0引脚
    # AINP = AIN0 and AINN = GND
    adc0 = ads1115.readVoltage(0)
  
    dat=abs(adc0['r']+19)
    print ("A0:%dmV "%(dat))
  
    if i==0:
        i=i+1
        if  dat<10:
            GPIO.output(4,GPIO.HIGH)
            GPIO.output(17,GPIO.HIGH)
            GPIO.output(27,GPIO.HIGH)
        elif dat<17:
            GPIO.output(4,GPIO.LOW)
            GPIO.output(17,GPIO.HIGH)
            GPIO.output(27,GPIO.HIGH)     
        elif dat<24:
            GPIO.output(4,GPIO.LOW)
            GPIO.output(17,GPIO.LOW)
            GPIO.output(27,GPIO.HIGH)     
        elif dat>30:
            GPIO.output(4,GPIO.LOW)
            GPIO.output(17,GPIO.LOW)
            GPIO.output(27,GPIO.LOW)          

    elif i==1:
        i=i+1
        if  dat<10:
            GPIO.output(18,GPIO.HIGH)
            GPIO.output(23,GPIO.HIGH)
            GPIO.output(24,GPIO.HIGH)
        elif dat<17:
            GPIO.output(18,GPIO.LOW)
            GPIO.output(23,GPIO.HIGH)
            GPIO.output(24,GPIO.HIGH)   
        elif dat<24:
            GPIO.output(18,GPIO.LOW)
            GPIO.output(23,GPIO.LOW)
            GPIO.output(24,GPIO.HIGH)     
        elif dat>30:
            GPIO.output(18,GPIO.LOW)
            GPIO.output(23,GPIO.LOW)
            GPIO.output(24,GPIO.LOW)   
            
    elif i==2:
        i=i+1
        if  dat<10:
            GPIO.output(16,GPIO.HIGH)
            GPIO.output(20,GPIO.HIGH)
            GPIO.output(21,GPIO.HIGH)
        elif dat<17:
            GPIO.output(16,GPIO.LOW)
            GPIO.output(20,GPIO.HIGH)
            GPIO.output(21,GPIO.HIGH)   
        elif dat<24:
            GPIO.output(16,GPIO.LOW)
            GPIO.output(20,GPIO.LOW)
            GPIO.output(21,GPIO.HIGH)     
        elif dat>30:
            GPIO.output(16,GPIO.LOW)
            GPIO.output(20,GPIO.LOW)
            GPIO.output(21,GPIO.LOW)   
            
    elif i==3:
        i=0
        if  dat<10:
            GPIO.output(13,GPIO.HIGH)
            GPIO.output(19,GPIO.HIGH)
            GPIO.output(26,GPIO.HIGH)
        elif dat<17:
            GPIO.output(13,GPIO.LOW)
            GPIO.output(19,GPIO.HIGH)
            GPIO.output(26,GPIO.HIGH)   
        elif dat<24:
            GPIO.output(13,GPIO.LOW)
            GPIO.output(19,GPIO.LOW)
            GPIO.output(26,GPIO.HIGH)     
        elif dat>30:
            GPIO.output(13,GPIO.LOW)
            GPIO.output(19,GPIO.LOW)
            GPIO.output(26,GPIO.LOW)               

    #time.sleep(0.2)
    #adc1 = ads1115.readVoltage(1)
    #time.sleep(0.2)
    #adc2 = ads1115.readVoltage(2)
    #time.sleep(0.2)
    #adc3 = ads1115.readVoltage(3)
    
    #print "A0:%dmV "%(adc0['r'])
